package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.heaven.cons.Roles;
import cn.ztydata.teachingsystem.heaven.entity.Student;
import cn.ztydata.teachingsystem.heaven.entity.Teacher;
import cn.ztydata.teachingsystem.heaven.entity.User;
import cn.ztydata.teachingsystem.heaven.service.StudentService;
import cn.ztydata.teachingsystem.heaven.service.TeacherService;
import cn.ztydata.teachingsystem.heaven.web.dto.StudentDto;
import cn.ztydata.teachingsystem.heaven.web.dto.TeacherDto;
import cn.ztydata.teachingsystem.heaven.web.dto.UserDto;
import cn.ztydata.teachingsystem.heaven.web.wrapper.HttpSessionProxy;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.UUID;

/**
 * 主控制器类
 *
 * @author fanruiping
 * @since 2014-12-02
 */
@RequestMapping("/index")
@Controller
public class IndexController extends BaseController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;

    @Value("${session.key}")
    private String sessionKey;

    /**
     * 教师登录
     * @param dto
     * @param request
     * @return JsonResult
     *
     * @author fanruiping
     * @since 2014-12-03
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult login(@RequestBody UserDto dto,HttpServletRequest request){
        HttpSessionProxy httpSessionProxy = (HttpSessionProxy)request.getSession();

        //生成sessionId
        String sessionId = UUID.randomUUID().toString();
        //初始化session
        httpSessionProxy.init(sessionId);

        int[] roleIds = {Roles.SUPER_ADMIN, Roles.COLLEGE_USER,Roles.DEPT_ADMIN,Roles.SPEC_LEADER,Roles.TEACHER};

        //判断type值是否正确
        if(0 > Arrays.binarySearch(roleIds, dto.getRoleId())){
            log.debug("roleId is wrong:0>Arrays.binarySearch(roleIds,dto.getRoleId())");

            return new JsonResult(false,"所选角色不存在");
        }

        //dto转为entity
        User user = mapper.map(dto, User.class);

        //判断登录信息
        Teacher teacher = teacherService.checkUserLogin(user);

        //判断是否获取用户信息
        if(null == teacher){
            log.debug("teacherInfo is not exist:null==teacher");

            return new JsonResult(false, "用户名或密码错误");
        }

        user.setUserId(teacher.getId());
        user.setRole(dto.getRoleId());
        user.setName(teacher.getName());

        httpSessionProxy.setAttribute(sessionKey, user);

        TeacherDto teacherDto = mapper.map(teacher, TeacherDto.class);
        teacherDto.setSessionId(sessionId);
        teacherDto.setPassword("");

        log.debug("teacherInfo is exist:null!=teacher");

        return new JsonResultData<>(teacherDto);
    }


    /**
     * 学生登录
     * @param dto
     * @param request
     * @return JsonResult
     *
     * @author fanruiping
     * @since 2015-04-06
     */
    @RequestMapping(value = "/studentLogin", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult studentLogin(@RequestBody UserDto dto,HttpServletRequest request){
        HttpSessionProxy httpSessionProxy = (HttpSessionProxy)request.getSession();

        //生成sessionId
        String sessionId = UUID.randomUUID().toString();
        //初始化session
        httpSessionProxy.init(sessionId);

        //dto转为entity
        User user = mapper.map(dto, User.class);

        //判断用户登录信息
        Student student = studentService.checkUserLogin(user);

        //判断是否获取用户信息
        if(null == student){
            log.debug("studentInfo is not exist:null==student");

            return new JsonResult(false, "用户名或密码错误");
        }

        user.setUserId(student.getId());
        user.setRole(Roles.STUDENT);
        user.setName(student.getName());

        httpSessionProxy.setAttribute(sessionKey, user);

        StudentDto studentDto = mapper.map(student, StudentDto.class);
        studentDto.setSessionId(sessionId);

        log.debug("studentInfo is exist:null!=student");

        return new JsonResultData<>(studentDto);
    }

    /**
     * 登出
     * @param sessionProxy
     * @return JsonResult
     *
     * @author fanruiping
     * @since 2014-12-09
     */
    @RequestMapping(value = "/loginOut", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult loginOut(HttpSessionProxy sessionProxy){

        sessionProxy.removeAttribute(sessionKey);

        return new JsonResult(true, "登出成功");
    }
}
